//************************************************************
//
// DATASTEP 4. FINANCIAL VARIABLES
//
// DESCRIPTION: DEFLATES AND WORK ON FINANCIAL STATEMENTS (BALANCE SHEET AND INCOME STATEMENTS)
//				
// AUTHOR: LIONEL NESTA
//
// DATASOURCE: COMPUSTAT
//
//*************************************************************

// PREAMBLE

clear
clear matrix 
clear mata
set more off
set mem 600m
set matsize 800
*cd "D:\Lionel Nesta\THOMAS\THOMAS_SPILLOVER\DATA"
pwd
cd ..\DATA


// UPLOADING ORIGINAL FINANCIAL STATEMENTS

use ALL_QUOTED_FIN.dta, clear
mmerge gvkey yeara using ALL_QUOTED_BAL.dta, type(1:1) unmatched(none)
drop _merge
rename yeara year

aorder
order gvkey year coname iname dnum cnum cic smbl naics 
sort gvkey year

save ALL_QUOTED.dta, replace

// RENAMING 

gen LI =  (data178 - data15) / data12

replace LI = 0 if LI < 0
replace LI = 1 if LI > 1

rename data1 LIQUID
rename data5 CURRENT_LIABILITIES
rename data29 L
rename data12 Y
rename data46 RD
rename data7 PPE_GROSS
rename data8 PPE_NET
rename data13 PI
rename data3 M
rename data6 TOT_ASSETS

// CLEANING AND RENAMING

keep  gvkey year dnum coname iname naics Y RD PPE_* TOT_ASSETS L M PI RD LIQUID CURRENT_LIABILITIES LI
order gvkey year dnum coname iname naics Y RD PPE_* TOT_ASSETS L M PI RD LIQUID CURRENT_LIABILITIES LI
sort gvkey year

// IMPORTING DEFLATOR US_GDP

mmerge year using gdp_def.dta, type(n:1) unmatched(master)
drop _merge

// DEFLATING

gen y = Y / def
gen rd = RD / def
gen ppe_gross = PPE_GROSS / def
gen ppe_net = PPE_NET / def
gen tot_assets = TOT_ASSETS / def
gen m = M / def
gen pi = PI / def
gen liquid = LIQUID / def
gen current_liabilities = CURRENT_LIABILITIES / def


// DECLARING PANEL

egen fid = group(gvkey)
order gvkey coname fid year 
tsset fid year
drop if y == .

/*
// IPOLATING


gen rdi = rd / y
sort gvkey year

by gvkey: ipolate rdi  year, gen(temp) 
sum rd
gen temp2 = temp * y
replace rd = temp2  if rd==. // & temp2 > r(min) & temp2 < r(max)
drop temp rdi
*/

// GENERATING SECTORS

gen sector = floor(dnum / 100)

// MARKET SHARE 

egen temp = sum(Y), by(sector year)
gen msh = Y / temp
drop temp

// TAKING LOGS

gen lny = ln(y)
gen lnk = ln(ppe_net)
gen lnl = ln(L)
gen lnm = ln(m)
gen lnrd = ln(rd)
gen lnpi = ln(pi)
gen lnliq = ln(liquid)
gen lncur_liab = ln(current_liabilities)
gen lnrdi = lnrd - lnk


// MARKET CONCENTRATION

egen market_conc = sum(msh^2), by(sector year)

// PRESAMPLE MEAN 

gen temp = lnrd if year < 1990

egen psm_lnrd = mean(temp), by(gvkey)

drop temp

gen temp = lnrdi if year < 1990

egen psm_lnrdi = mean(temp), by(gvkey)

drop temp
 
 // AND SAVING
 
save fin_var.dta, replace
